package com.example.springboot.controller.web;

import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 功能
 * 作者：八滴水
 * 日期： 2024/11/19 23:56
 */
@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    UserService userService;
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.insertUser(user);
        return Result.success();
    }
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user =userService.selectById(id);
        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("用户不存在");
        }
    }
    @GetMapping("/selectAll")
    public List<User> selectAll() {
        List<User> users = userService.selectAll();
        return users;
    }
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }
}
